package com.yuanqi.repository;

import com.yuanqi.domain.ManualPayment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface ManualPaymentRepository extends JpaRepository<ManualPayment, Long> {
    
    /**
     * 根据付款编号查找
     */
    Optional<ManualPayment> findByPaymentCode(String paymentCode);
    
    /**
     * 根据供应商ID查找
     */
    List<ManualPayment> findBySupplierId(Long supplierId);
    
    /**
     * 根据状态查找
     */
    List<ManualPayment> findByStatus(Integer status);
    
    /**
     * 根据付款方式查找
     */
    List<ManualPayment> findByPaymentMethod(String paymentMethod);
    
    /**
     * 根据创建时间范围查找
     */
    @Query("SELECT mp FROM ManualPayment mp WHERE mp.createTime BETWEEN :startDate AND :endDate")
    List<ManualPayment> findByCreateTimeBetween(@Param("startDate") String startDate, @Param("endDate") String endDate);
    
    /**
     * 查找最大的付款编号数字部分
     */
    @Query("SELECT MAX(CAST(SUBSTRING(mp.paymentCode, 3) AS integer)) FROM ManualPayment mp WHERE mp.paymentCode LIKE 'MP%'")
    Integer findMaxPaymentCodeNumber();
    
    /**
     * 根据状态统计数量
     */
    long countByStatus(Integer status);
    
    /**
     * 根据供应商名称模糊查询
     */
    List<ManualPayment> findBySupplierNameContaining(String supplierName);
}

